using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    public partial class UC_Keyword : UserControl
    {
        public UC_Keyword()
        {
            InitializeComponent();
        }

        public void LoadData(string DocumentTitle, string DOC_ID, string keywordlist)
        {
            groupBox_DocKeywords.Text = "Keywords for Document " + DocumentTitle + "(" + DOC_ID + ")";

            LoadKeywordList(keywordlist);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string keyword = txtKeyword.Text.Trim();

            if (!string.IsNullOrEmpty(keyword))
            {
                if (!lsv_Keywords.IsItemAlreadyInList(keyword))
                {
                    ListViewItem aitem = new ListViewItem(keyword);
                    aitem.Tag = keyword;
                    lsv_Keywords.Items.Add(aitem);
                }
                else
                {
                    MessageBox.Show(keyword + " already in the list.");
                }
            }
        }

        public void LoadKeywordList(string list)
        {
            if (list.Trim() != "" && list.Trim() != "<NULL>")
            {
                string[] tokens = list.Split(new char[] { ',' });
                foreach (string token in tokens)
                {
                    if (!string.IsNullOrEmpty(token.Trim()))
                    {
                        ListViewItem aitem = new ListViewItem(token.Trim());
                        aitem.Tag = token.Trim();
                        lsv_Keywords.Items.Add(aitem);
                    }
                }
            }
            else
            {
                groupBox_DocKeywords.Text = "No Keywords";
            }
        }

        public string GetKeywordList()
        {
            string keywordlist = "";

            if (lsv_Keywords.Items.Count > 0)
            {
                foreach (ListViewItem aitem in lsv_Keywords.Items)
                {
                    string keyword = "";

                    if (aitem.Tag != null)
                    {
                        keyword = aitem.Tag.ToString().Trim();

                        if (string.IsNullOrEmpty(keywordlist))
                            keywordlist = keyword;
                        else
                            keywordlist += ", " + keyword;
                    }
                }
            }

            return keywordlist;
        }

        private void lsv_Keywords_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && lsv_Keywords.SelectedItems.Count > 0)
            {
                foreach (ListViewItem aitem in lsv_Keywords.SelectedItems)
                {
                    lsv_Keywords.Items.Remove(aitem);
                }
            }
        }
    }
}
